// 格式化发布时间
function timeago(datetime) { //dateTimeStamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。

	var dateTimeStamp = Date.parse(datetime.replace(/-/gi, "/"));
	// var dateTimeStamp =(new Date(datetime)).getTime()
	var minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
	var hour = minute * 60;
	var day = hour * 24;
	var week = day * 7;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime(); //获取当前时间毫秒
	var diffValue = now - dateTimeStamp; //时间差
	if (diffValue < 0) {
		return '刚刚';
	}
	var minC = parseInt(diffValue / minute); //计算时间差的分，时，天，周，月
	var hourC = parseInt(diffValue / hour);
	var dayC = parseInt(diffValue / day);
	var weekC = parseInt(diffValue / week);
	var monthC = parseInt(diffValue / month);
	var result = "2";
	
	if (monthC >= 1 && monthC <= 6) {
		result = " " + parseInt(monthC) + "月前"
	} else if (monthC > 6 && monthC < 12) {
		result = "半年前"
	} else if (monthC >= 12) {
		result = "一年前"
	} else if (weekC >= 1 && weekC <= 4) {
		result = " " + parseInt(weekC) + "周前"
	} else if (dayC >= 1 && dayC <= 6) {
		result = " " + parseInt(dayC) + "天前"
	} else if (hourC >= 1 && hourC <= 23) {
		result = " " + parseInt(hourC) + "小时前"
	} else if (minC >= 1 && minC <= 59) {
		result = " " + parseInt(minC) + "分钟前"
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = "刚刚"
	} else {
		var datetime = new Date();
		datetime.setTime(dateTimeStamp);
		var Nyear = datetime.getFullYear(); {}
		var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		result = Nyear + "-" + Nmonth + "-" + Ndate + ' ' + Nhour + ':' + Nminute + ':' + Nsecond
	}
	return result;
}

// 格式化阅读量
function formatReadNum(num) {
	if (num < 10000) {
		return num
	}

	let w = num / 10000
	w = w.toFixed(1)
	return w + '万'
}

function debounce(func, delay) {
	let timer;
	return function() {
		clearTimeout(timer);
		timer = setTimeout(() => {
			func.apply(this, arguments);
		}, delay);
	};
}

function throttle(func, delay) {
	let timer;
	let lastCallTime = 0

	return function() {
		const now = Date.now()

		if (now - lastCallTime >= delay) {
			func.apply(this, arguments)
			lastCallTime = now
		} else {
			clearTimeout(timer)

			timer = setTimeout(() => {
				func.apply(this, arguments);
				lastCallTime = now;
			}, delay);
		}
	};
}

function toolToPage(url) {
	uni.navigateTo({
		url,
		fail() {
			console.log('页面跳转失败，url：', url)
		}
	})
}

/**
 * @description: 检测字符串中字母是否连续
 * @param {*} str
 * @return
 */
function isConsecutiveLetters(str) {
  str = str.toLowerCase(); // 将字符串转换为小写字母
  for (let i = 0; i < str.length - 1; i++) {
    let currentCharCode = str.charCodeAt(i);
    let nextCharCode = str.charCodeAt(i + 1);
    if (nextCharCode - currentCharCode !== 1) {
      return false; // 如果不是连续字母，则返回false
    }
  }
  return true; // 如果所有字母都是连续的，则返回true
}

const api = {
	timeago: datetime => timeago(datetime),
	formatReadNum: num => formatReadNum(num),
	debounce: (func, delay) => debounce(func, delay),
	throttle: (func, delay) => throttle(func, delay),
	toolToPage: url => toolToPage(url),
	isConsecutiveLetters: (str) => isConsecutiveLetters(str)
}

// export {timeago, formatReadNum}
export default api